using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace pxt
{
    public class AudioSystem : Microsoft.Xna.Framework.GameComponent
    {
        private AudioEngine audioEngine;
        private SoundBank soundBank;
        private WaveBank waveBank;
        private Dictionary<string, Cue> cueLookup;

        public AudioSystem(Game game) : base(game)
        {
            // TODO: Construct any child components here
        }

        public override void Initialize()
        {
            // TODO: Add your initialization code here
            audioEngine = new AudioEngine("Content\\audio\\mwt_audio.xgs");
            waveBank = new WaveBank(audioEngine, "Content\\audio\\Wave Bank.xwb");
            soundBank = new SoundBank(audioEngine, "Content\\audio\\Sound Bank.xsb");

            // Init other members
            cueLookup = new Dictionary<string, Cue>();

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            audioEngine.Update();
            base.Update(gameTime);
        }

        public void PlaySoundCue(string cue_name)
        {
            Cue cue ;
            if (false == cueLookup.ContainsKey(cue_name))
            {
                cue = soundBank.GetCue(cue_name);
                cueLookup.Add(cue_name, cue);
                cue.Play();
            }
            else
            {
                if (cueLookup[cue_name].IsPaused)
                {
                    cueLookup[cue_name].Resume();
                }
                else
                {
                    cue = soundBank.GetCue(cue_name);
                    cue.Play();
                }
            }
        }

        public void StopSoundCue(string cue_name)
        {
            if (cueLookup.ContainsKey(cue_name))
            {
                Cue cue = cueLookup[cue_name];
                if (cue.IsPlaying)
                {
                    cue.Stop(AudioStopOptions.Immediate);
                    cueLookup.Remove(cue_name);
                }
            }
        }

        public void PauseSoundCue(string cue_name)
        {
            if (cueLookup.ContainsKey(cue_name))
            {
                Cue cue = cueLookup[cue_name];
                if (cue.IsPlaying)
                {
                    cue.Pause();
                }
            }
        }
    }
}